//
//  JokesViewController.m
//  Connotation
//
//  Created by LZXuan on 15-4-7.
//  Copyright (c) 2015年 LZXuan. All rights reserved.
//

#import "JokesViewController.h"

@interface JokesViewController ()

@end

@implementation JokesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.Nav_title.text=@"段子";
    [self isNetWork];//检测网络状态，解归档
}
-(void)isNetWork{
    BOOL isnet=[LZXHelper reachability];
    if(isnet){
        //第一次下载
        _currentPage = 0;
        self.max_timestamp = @"-1";
        [self loadDataPage:_currentPage count:15];
        
        //再创建刷新
        [self creatRefreshView];
        //点击 加载更多
        [self upPullView];
    }else {
        [self unarchiveMyData:@"jokes"];
        [_tableView reloadData];
    }
}

- (void)downloadDataSuccess:(LZXHttpRequest *)request {
    
    if (request.downloadData) {
        
        
        if (_currentPage == 0) {//下拉刷新
            [self.dataArr removeAllObjects];
        }
        
        //解析数据  看数据 是xml 还是json
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.downloadData options:NSJSONReadingMutableContainers error:nil];
        NSArray *itemArr = dict[@"items"];
        //遍历数组
        for (NSDictionary *itemDict in itemArr) {
            //把数据 保存到model 中
            PicModel *model = [[PicModel alloc] init];
            
            /*
             kvc会根据itemDict中的key 去model 找对应的属性 ，然后把itemDict中value 赋给相应地model属性值，
             如果itemDict字典的key 在model 中找不到对应的 属性名
             那么会调用 - (void)setValue:(id)value forUndefinedKey:(NSString *)key
             */
            
            //使用kvc 同时对多个属性赋值
            [model setValuesForKeysWithDictionary:itemDict];
            //加入数据源数组
            [self.dataArr addObject:model];
            [model release];
        }
        [self archiveMyData:@"jokes"];
        //刷新 表格
        [self.tableView reloadData];
        _isLoadMore = NO;
        _isRefreshing = NO;
        //ego 停止刷新
        [self.headerView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
        
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
